package com.android.nearshop.app.activity.main;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.common.LoadableActivity;
import com.android.nearshop.app.adapter.listadapter.ArrayExpandListAdapter.ExpandData;
import com.android.nearshop.app.adapter.listadapter.CategoryListAdapter;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.app.widget.TitleBar.OnTitleClickListener;
import com.android.nearshop.business.cache.DataCacheManager;
import com.android.nearshop.io.protocol.out.Catalogy;
import com.android.nearshop.util.Constant;
import com.android.nearshop.util.NotifyListener;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-16
 * @see : 商家分类选择，可分单选 与 多选
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class SelCategoryActivity extends LoadableActivity 
implements OnChildClickListener,OnGroupClickListener
{
    public static final int CODE_REQUEST = 0x8803;
    private CategoryListAdapter listAdapter;
    private boolean isMutilSelMode;
    private ArrayList<Integer> selectedList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        isMutilSelMode = getIntent().getBooleanExtra(Constant.TAG_ISMUTILSEL, false);  
        selectedList   = getIntent().getIntegerArrayListExtra(Constant.TAG_CATEGORYS);
        setContentView(R.layout.view_expand_list);
        ExpandableListView expandList = (ExpandableListView)findViewById(R.id.expandableListView);
        listAdapter = new CategoryListAdapter(this,isMutilSelMode);
        expandList.setAdapter(listAdapter);
        expandList.setOnChildClickListener(this);
        expandList.setOnGroupClickListener(this);
        loadData();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(R.string.title_shopcate);        
        if(isMutilSelMode)
        {
            titleBar.setRigthText(R.string.sure);
            titleBar.setOnTitleClickListener(new OnTitleClickListener()
            {
                @Override
                public void onRightClick()
                {
                    onMutilOK();
                }
            }, false, true);
        }else
        {
            titleBar.setRightVisible(false);
        }
    }
    
    private void loadData()
    {
        mLoadingView.showLoadingView();
        DataCacheManager.getInstance().getCateList(
                new NotifyListener<List<Catalogy>>()
        {
            @Override
            public void onSucceed(List<Catalogy> result)
            {
                if(result != null)
                {
                    int size = result.size();
                    List<ExpandData<Catalogy, Catalogy>> list = new ArrayList<ExpandData<Catalogy,Catalogy>>(size + 1);
                    ExpandData<Catalogy, Catalogy> one = null;
                    // all header
                    one = listAdapter.getAllHeader();
                    haveSelcted((Catalogy)one.group);
                    list.add(one);
                    // other category
                    for(int i = 0; i< size; i++)
                    {
                        haveSelcted(result.get(i));
                        one = new ExpandData<Catalogy, Catalogy>(result.get(i), result.get(i).catalogyList);
                        list.add(one);
                    }                    
                    listAdapter.setList(list);
                    mLoadingView.showMainView();
                }else
                {
                    mLoadingView.showLoadingErrView(R.string.no_result,false);
                }
            }
            @Override
            public void onError(int resultCode)
            {
                mLoadingView.showLoadingErrView(false);
            }
        });
    }
    
    private void haveSelcted(Catalogy group)
    {
        if(selectedList != null && selectedList.size() > 0)
        {
            if(selectedList.contains(group.catalogyId))
            {
                group.checked(false, false, true);
            }else
            {
                if(group.catalogyList != null && group.catalogyList.size() > 0)
                {
                    for(Catalogy child : group.catalogyList)
                    {
                        if(selectedList.contains(child.catalogyId))
                        {
                            child.checked(false, false, true);
                        }
                    }
                }
            }
        }
    }
    
    private void onMutilOK()
    {
        Intent intent = new Intent();
        intent.putExtra(Constant.TAG_CATEGORYS, listAdapter.getSelIds());
        setResult(RESULT_OK, intent);
        finish();
    }
    
    private void onSinlOK(int id)
    {
        Intent intent = new Intent();
        ArrayList<Integer> list = new ArrayList<Integer>(1);
        list.add(id);
        intent.putIntegerArrayListExtra(Constant.TAG_CATEGORYS, list);
        setResult(RESULT_OK, intent);
        finish();
    }
    
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, long id)
    {
        if(!isMutilSelMode && groupPosition == 0)
        {
            onSinlOK(-1);
        }
        return false;
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id)
    {
        if(!isMutilSelMode)
        {
            onSinlOK( ((Catalogy)listAdapter.getChild(groupPosition, childPosition)).catalogyId);
        }
        return false;
    }
    
    @Override
    public void refreshView()
    {
        loadData();
    }
    
    /**
     * @param context
     * @param isMutilSel 是否是多选
     */
    public static void startActivityResult(Activity context,boolean isMutilSel)
    {        
        context.startActivityForResult(getStartActivity(context,isMutilSel,null), CODE_REQUEST);
    }
    
    /**
     * @param context
     * @param isMutilSel 是否是多选
     * @param selectedList 已选id列表
     */
    public static void startActivityResult(Activity context,boolean isMutilSel, ArrayList<Integer> selectedList)
    {        
        context.startActivityForResult(getStartActivity(context,isMutilSel,selectedList), CODE_REQUEST);
    }
       
    public static Intent getStartActivity(Activity context,boolean isMutilSel, ArrayList<Integer> selectedList)
    {        
        Intent intent = new Intent(context, SelCategoryActivity.class);
        intent.putExtra(Constant.TAG_ISMUTILSEL, isMutilSel);
        if(selectedList != null && selectedList.size() > 0)
        {
            intent.putIntegerArrayListExtra(Constant.TAG_CATEGORYS, selectedList);
        }
        return intent;
    }
}
